package com.imooc.miaosha.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {

    private int code;
    private String msg;
    private T data;

    /**
     * 成功时候的调用
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>(data);
    }

    public static <T> Result<T> successMsg(String msg, T data) {
        return new Result<T>(0, msg, data);
    }

    public static <T> Result<T> successMsg(String msg) {
        return new Result<T>(0, msg);
    }

    /**
     * 失败时候的调用
     */
    public static <T> Result<T> error(Const aConst) {
        return new Result<T>(aConst);
    }

    private Result(T data) {
        this.data = data;
    }

    private Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private Result(Const aConst) {
        if (aConst != null) {
            this.code = aConst.getCode();
            this.msg = aConst.getMsg();
        }
    }
}
